<?
	include("../main_inc.php");
	
	if($_POST['Submit']){
		
		if(is_array($_POST['DFiles'])){
			foreach($_POST['DFiles'] as $val){
				$r->RawQuery("DELETE FROM affiliateJoinedPrograms WHERE affiliateID=$_SESSION[AffKey] AND affiliateProgramsID=$val");
				$rslt=$r->RawQuery("SELECT AutoApproval FROM affiliatePrograms WHERE id=$val");
				$data=mysql_fetch_array($rslt);
				$Status=($data[0]=="Y" ? "Approved":"Requested"); 
				$r->RawQuery("INSERT INTO affiliateJoinedPrograms (affiliateID,affiliateProgramsID,Status) VALUES ($_SESSION[AffKey],$val,'$Status')");
			}
		}
		$Message="Program Requested/Joined";
		
	};
	
	
	
	$PInsert=array();
	$PStatus=array();
	$Count=1;
	$rslt=$r->RawQuery("SELECT affiliateProgramsID,Status FROM affiliateJoinedPrograms WHERE affiliateID=".$_SESSION['AffKey']);
	while($data= mysql_fetch_row($rslt)){
		$PInsert[$Count]=$data[0];
		$PStatus[$Count]=$data[1];
		$Count++;
	}

?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Bubble Affiliate Program</title>

<link href="../../css/admin.css" rel="stylesheet" type="text/css">
<script src="../../jscript/menuArrayAffiliate.php" language="javascript"></script>
<script src="../../jscript/clock.js" language="javascript"></script>
</head>

<body onLoad="funClock();">
<?php include("../header.php"); ?>
<table width="100%" border="0" cellspacing="25" cellpadding="0">
<tr>
<td width="240" height="272" valign="top"><div id="menu"><?php include("../menu.php"); ?></div></td>
<td valign="top" align="center">

<?=$Message;?>
<div id="titleCompact">Programs List  </div>
<div id="contentCompact">
<form name="form1" method="post" action="view-programs.php">
  <table width="600" border="0" cellpadding="5" cellspacing="1" bgcolor="#E9EEF5">
    <tr id="formHeader">
      <td width="192"><strong>Name </strong></td>
      <td width="40" align="center"><strong>Click</strong></td>
      <td width="37" align="center"><strong>Lead</strong></td>
      <td width="41" align="center"><strong>Sale</strong></td>
      <td width="85" align="center"><strong>Banners</strong></td>
      <td width="84" align="center"><strong>Status</strong></td>
      <td width="43" align="center"><strong>Join</strong></td>
    </tr>
    <?php
	$Count=0;
	$r=new ReturnRecord();
	$sq2=$r->rawQuery("SELECT id,Name,CCommission,LCommission,SCommission,SaleType FROM affiliatePrograms");  
	while ($myrow = mysql_fetch_row($sq2)) {
?>
    <tr bgcolor="<?=(($Count%2)==0 ? "#FFFFFF" : "#EBEBEB"); ?>">
      <td><?=$myrow[1];?></td>
      <td align="center"><?=$myrow[2];?></td>
      <td align="center"><?=$myrow[3];?></td>
      <td align="center"><?=$myrow[4];?>
          <?=$myrow[5];?></td>
      <td align="center">
	  		<?
			$pkey=array_search($myrow[0],$PInsert);
			if($PStatus[$pkey]=="Approved"){
			?>
	  		<a href="get-codes.php?id=<?=$myrow[0];?>">Get Codes </a>
			<? };?>
	  </td>
      <td align="center"><?
			
			if($PStatus[$pkey]){
				print $PStatus[$pkey];
			}else{
				?>
        Not Joined
          <?
			}
			?></td>
      <td align="center">
	  <? if(!$pkey){?>
	  <input type="checkbox" name="DFiles[]2" value="<?=$myrow[0];?>">
	  <? };?>
	  </td>
    </tr>
    <?
		$Count++;
	};
?>
    <tr>
      <td colspan="6" bgcolor="#FFFFFF">&nbsp;</td>
      <td align="center" bgcolor="#FFFFFF"><input name="Submit" type="submit" id="Submit" value="Join" class="formButtonSubmit"></td>
    </tr>
  </table>
  </form>
</div></td>
</tr>
</table>
</body>
</html>
